home *** CD-ROM | disk | FTP | other *** search
- Path: abcfd20.larc.nasa.gov!amiga-request
- From: amiga-request@abcfd20.larc.nasa.gov (Amiga Sources/Binaries Moderator)
- Subject: v90i234: flex 2.3 - fast lexical analyzer generator, Part07/13
- Reply-To: loftus@wpllabs.uucp (William P Loftus)
- Newsgroups: comp.sources.amiga
- Message-ID: <comp.sources.amiga:v90i234@abcfd20.larc.nasa.gov>
- Date: 19 Aug 90 22:42:50 GMT
- Approved: tadguy@uunet.UU.NET (Tad Guy)
- X-Mail-Submissions-To: amiga@uunet.uu.net
- X-Post-Discussions-To: comp.sys.amiga
-
- Submitted-by: loftus@wpllabs.uucp (William P Loftus)
- Posting-number: Volume 90, Issue 234
- Archive-name: unix/flex-2.3/part07
-
- #!/bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 7 (of 13)."
- # Contents: gen.c
- # Wrapped by tadguy@abcfd20 on Sun Aug 19 18:41:46 1990
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'gen.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'gen.c'\"
- else
- echo shar: Extracting \"'gen.c'\" \(31559 characters\)
- sed "s/^X//" >'gen.c' <<'END_OF_FILE'
- X/* gen - actual generation (writing) of flex scanners */
- X
- X/*-
- X * Copyright (c) 1990 The Regents of the University of California.
- X * All rights reserved.
- X *
- X * This code is derived from software contributed to Berkeley by
- X * Vern Paxson.
- X *
- X * The United States Government has rights in this work pursuant
- X * to contract no. DE-AC03-76SF00098 between the United States
- X * Department of Energy and the University of California.
- X *
- X * Redistribution and use in source and binary forms are permitted provided
- X * that: (1) source distributions retain this entire copyright notice and
- X * comment, and (2) distributions including binaries display the following
- X * acknowledgement: ``This product includes software developed by the
- X * University of California, Berkeley and its contributors'' in the
- X * documentation or other materials provided with the distribution and in
- X * all advertising materials mentioning features or use of this software.
- X * Neither the name of the University nor the names of its contributors may
- X * be used to endorse or promote products derived from this software without
- X * specific prior written permission.
- X * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- X * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- X * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
- X */
- X
- X#ifndef lint
- Xstatic char rcsid[] =
- X "@(#) $Header: /usr/fsys/odin/a/vern/flex/RCS/gen.c,v 2.9 90/06/27 23:48:22 vern Exp $ (LBL)";
- X#endif
- X
- X#include "flexdef.h"
- X
- X
- X/* declare functions that have forward references */
- X
- Xvoid gen_next_state PROTO((int));
- Xvoid genecs PROTO(());
- Xvoid indent_put2s PROTO((char [], char []));
- Xvoid indent_puts PROTO((char []));
- X
- X
- Xstatic int indent_level = 0; /* each level is 4 spaces */
- X
- X#define indent_up() (++indent_level)
- X#define indent_down() (--indent_level)
- X#define set_indent(indent_val) indent_level = indent_val
- X
- X/* *everything* is done in terms of arrays starting at 1, so provide
- X * a null entry for the zero element of all C arrays
- X */
- Xstatic char C_short_decl[] = "static const short int %s[%d] =\n { 0,\n";
- Xstatic char C_long_decl[] = "static const long int %s[%d] =\n { 0,\n";
- Xstatic char C_state_decl[] =
- X "static const yy_state_type %s[%d] =\n { 0,\n";
- X
- X
- X/* indent to the current level */
- X
- Xvoid do_indent()
- X
- X {
- X register int i = indent_level * 4;
- X
- X while ( i >= 8 )
- X {
- X putchar( '\t' );
- X i -= 8;
- X }
- X
- X while ( i > 0 )
- X {
- X putchar( ' ' );
- X --i;
- X }
- X }
- X
- X
- X/* generate the code to keep backtracking information */
- X
- Xvoid gen_backtracking()
- X
- X {
- X if ( reject || num_backtracking == 0 )
- X return;
- X
- X if ( fullspd )
- X indent_puts( "if ( yy_current_state[-1].yy_nxt )" );
- X else
- X indent_puts( "if ( yy_accept[yy_current_state] )" );
- X
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_last_accepting_state = yy_current_state;" );
- X indent_puts( "yy_last_accepting_cpos = yy_cp;" );
- X indent_puts( "}" );
- X indent_down();
- X }
- X
- X
- X/* generate the code to perform the backtrack */
- X
- Xvoid gen_bt_action()
- X
- X {
- X if ( reject || num_backtracking == 0 )
- X return;
- X
- X set_indent( 3 );
- X
- X indent_puts( "case 0: /* must backtrack */" );
- X indent_puts( "/* undo the effects of YY_DO_BEFORE_ACTION */" );
- X indent_puts( "*yy_cp = yy_hold_char;" );
- X
- X if ( fullspd || fulltbl )
- X indent_puts( "yy_cp = yy_last_accepting_cpos + 1;" );
- X else
- X /* backtracking info for compressed tables is taken \after/
- X * yy_cp has been incremented for the next state
- X */
- X indent_puts( "yy_cp = yy_last_accepting_cpos;" );
- X
- X indent_puts( "yy_current_state = yy_last_accepting_state;" );
- X indent_puts( "goto yy_find_action;" );
- X putchar( '\n' );
- X
- X set_indent( 0 );
- X }
- X
- X
- X/* genctbl - generates full speed compressed transition table
- X *
- X * synopsis
- X * genctbl();
- X */
- X
- Xvoid genctbl()
- X
- X {
- X register int i;
- X int end_of_buffer_action = num_rules + 1;
- X
- X /* table of verify for transition and offset to next state */
- X printf( "static const struct yy_trans_info yy_transition[%d] =\n",
- X tblend + numecs + 1 );
- X printf( " {\n" );
- X
- X /* We want the transition to be represented as the offset to the
- X * next state, not the actual state number, which is what it currently is.
- X * The offset is base[nxt[i]] - base[chk[i]]. That's just the
- X * difference between the starting points of the two involved states
- X * (to - from).
- X *
- X * first, though, we need to find some way to put in our end-of-buffer
- X * flags and states. We do this by making a state with absolutely no
- X * transitions. We put it at the end of the table.
- X */
- X /* at this point, we're guaranteed that there's enough room in nxt[]
- X * and chk[] to hold tblend + numecs entries. We need just two slots.
- X * One for the action and one for the end-of-buffer transition. We
- X * now *assume* that we're guaranteed the only character we'll try to
- X * index this nxt/chk pair with is EOB, i.e., 0, so we don't have to
- X * make sure there's room for jam entries for other characters.
- X */
- X
- X base[lastdfa + 1] = tblend + 2;
- X nxt[tblend + 1] = end_of_buffer_action;
- X chk[tblend + 1] = numecs + 1;
- X chk[tblend + 2] = 1; /* anything but EOB */
- X nxt[tblend + 2] = 0; /* so that "make test" won't show arb. differences */
- X
- X /* make sure every state has a end-of-buffer transition and an action # */
- X for ( i = 0; i <= lastdfa; ++i )
- X {
- X register int anum = dfaacc[i].dfaacc_state;
- X
- X chk[base[i]] = EOB_POSITION;
- X chk[base[i] - 1] = ACTION_POSITION;
- X nxt[base[i] - 1] = anum; /* action number */
- X }
- X
- X for ( i = 0; i <= tblend; ++i )
- X {
- X if ( chk[i] == EOB_POSITION )
- X transition_struct_out( 0, base[lastdfa + 1] - i );
- X
- X else if ( chk[i] == ACTION_POSITION )
- X transition_struct_out( 0, nxt[i] );
- X
- X else if ( chk[i] > numecs || chk[i] == 0 )
- X transition_struct_out( 0, 0 ); /* unused slot */
- X
- X else /* verify, transition */
- X transition_struct_out( chk[i], base[nxt[i]] - (i - chk[i]) );
- X }
- X
- X
- X /* here's the final, end-of-buffer state */
- X transition_struct_out( chk[tblend + 1], nxt[tblend + 1] );
- X transition_struct_out( chk[tblend + 2], nxt[tblend + 2] );
- X
- X printf( " };\n" );
- X printf( "\n" );
- X
- X /* table of pointers to start states */
- X printf( "static const struct yy_trans_info *yy_start_state_list[%d] =\n",
- X lastsc * 2 + 1 );
- X printf( " {\n" );
- X
- X for ( i = 0; i <= lastsc * 2; ++i )
- X printf( " &yy_transition[%d],\n", base[i] );
- X
- X dataend();
- X
- X if ( useecs )
- X genecs();
- X }
- X
- X
- X/* generate equivalence-class tables */
- X
- Xvoid genecs()
- X
- X {
- X register int i, j;
- X static char C_char_decl[] = "static const %s %s[%d] =\n { 0,\n";
- X int numrows;
- X Char clower();
- X
- X if ( numecs < csize )
- X printf( C_char_decl, "YY_CHAR", "yy_ec", csize );
- X else
- X printf( C_char_decl, "short", "yy_ec", csize );
- X
- X for ( i = 1; i < csize; ++i )
- X {
- X if ( caseins && (i >= 'A') && (i <= 'Z') )
- X ecgroup[i] = ecgroup[clower( i )];
- X
- X ecgroup[i] = abs( ecgroup[i] );
- X mkdata( ecgroup[i] );
- X }
- X
- X dataend();
- X
- X if ( trace )
- X {
- X char *readable_form();
- X
- X fputs( "\n\nEquivalence Classes:\n\n", stderr );
- X
- X numrows = csize / 8;
- X
- X for ( j = 0; j < numrows; ++j )
- X {
- X for ( i = j; i < csize; i = i + numrows )
- X {
- X fprintf( stderr, "%4s = %-2d", readable_form( i ), ecgroup[i] );
- X
- X putc( ' ', stderr );
- X }
- X
- X putc( '\n', stderr );
- X }
- X }
- X }
- X
- X
- X/* generate the code to find the action number */
- X
- Xvoid gen_find_action()
- X
- X {
- X if ( fullspd )
- X indent_puts( "yy_act = yy_current_state[-1].yy_nxt;" );
- X
- X else if ( fulltbl )
- X indent_puts( "yy_act = yy_accept[yy_current_state];" );
- X
- X else if ( reject )
- X {
- X indent_puts( "yy_current_state = *--yy_state_ptr;" );
- X indent_puts( "yy_lp = yy_accept[yy_current_state];" );
- X
- X puts( "find_rule: /* we branch to this label when backtracking */" );
- X
- X indent_puts( "for ( ; ; ) /* until we find what rule we matched */" );
- X
- X indent_up();
- X
- X indent_puts( "{" );
- X
- X indent_puts( "if ( yy_lp && yy_lp < yy_accept[yy_current_state + 1] )" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_act = yy_acclist[yy_lp];" );
- X
- X if ( variable_trailing_context_rules )
- X {
- X indent_puts( "if ( yy_act & YY_TRAILING_HEAD_MASK ||" );
- X indent_puts( " yy_looking_for_trail_begin )" );
- X indent_up();
- X indent_puts( "{" );
- X
- X indent_puts( "if ( yy_act == yy_looking_for_trail_begin )" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_looking_for_trail_begin = 0;" );
- X indent_puts( "yy_act &= ~YY_TRAILING_HEAD_MASK;" );
- X indent_puts( "break;" );
- X indent_puts( "}" );
- X indent_down();
- X
- X indent_puts( "}" );
- X indent_down();
- X
- X indent_puts( "else if ( yy_act & YY_TRAILING_MASK )" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts(
- X "yy_looking_for_trail_begin = yy_act & ~YY_TRAILING_MASK;" );
- X indent_puts(
- X "yy_looking_for_trail_begin |= YY_TRAILING_HEAD_MASK;" );
- X
- X if ( real_reject )
- X {
- X /* remember matched text in case we back up due to REJECT */
- X indent_puts( "yy_full_match = yy_cp;" );
- X indent_puts( "yy_full_state = yy_state_ptr;" );
- X indent_puts( "yy_full_lp = yy_lp;" );
- X }
- X
- X indent_puts( "}" );
- X indent_down();
- X
- X indent_puts( "else" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_full_match = yy_cp;" );
- X indent_puts( "yy_full_state = yy_state_ptr;" );
- X indent_puts( "yy_full_lp = yy_lp;" );
- X indent_puts( "break;" );
- X indent_puts( "}" );
- X indent_down();
- X
- X indent_puts( "++yy_lp;" );
- X indent_puts( "goto find_rule;" );
- X }
- X
- X else
- X {
- X /* remember matched text in case we back up due to trailing context
- X * plus REJECT
- X */
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_full_match = yy_cp;" );
- X indent_puts( "break;" );
- X indent_puts( "}" );
- X indent_down();
- X }
- X
- X indent_puts( "}" );
- X indent_down();
- X
- X indent_puts( "--yy_cp;" );
- X
- X /* we could consolidate the following two lines with those at
- X * the beginning, but at the cost of complaints that we're
- X * branching inside a loop
- X */
- X indent_puts( "yy_current_state = *--yy_state_ptr;" );
- X indent_puts( "yy_lp = yy_accept[yy_current_state];" );
- X
- X indent_puts( "}" );
- X
- X indent_down();
- X }
- X
- X else
- X /* compressed */
- X indent_puts( "yy_act = yy_accept[yy_current_state];" );
- X }
- X
- X
- X/* genftbl - generates full transition table
- X *
- X * synopsis
- X * genftbl();
- X */
- X
- Xvoid genftbl()
- X
- X {
- X register int i;
- X int end_of_buffer_action = num_rules + 1;
- X
- X printf( C_short_decl, "yy_accept", lastdfa + 1 );
- X
- X
- X dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
- X
- X for ( i = 1; i <= lastdfa; ++i )
- X {
- X register int anum = dfaacc[i].dfaacc_state;
- X
- X mkdata( anum );
- X
- X if ( trace && anum )
- X fprintf( stderr, "state # %d accepts: [%d]\n", i, anum );
- X }
- X
- X dataend();
- X
- X if ( useecs )
- X genecs();
- X
- X /* don't have to dump the actual full table entries - they were created
- X * on-the-fly
- X */
- X }
- X
- X
- X/* generate the code to find the next compressed-table state */
- X
- Xvoid gen_next_compressed_state( char_map )
- Xchar *char_map;
- X
- X {
- X indent_put2s( "register YY_CHAR yy_c = %s;", char_map );
- X
- X /* save the backtracking info \before/ computing the next state
- X * because we always compute one more state than needed - we
- X * always proceed until we reach a jam state
- X */
- X gen_backtracking();
- X
- X indent_puts(
- X "while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_current_state = yy_def[yy_current_state];" );
- X
- X if ( usemecs )
- X {
- X /* we've arrange it so that templates are never chained
- X * to one another. This means we can afford make a
- X * very simple test to see if we need to convert to
- X * yy_c's meta-equivalence class without worrying
- X * about erroneously looking up the meta-equivalence
- X * class twice
- X */
- X do_indent();
- X /* lastdfa + 2 is the beginning of the templates */
- X printf( "if ( yy_current_state >= %d )\n", lastdfa + 2 );
- X
- X indent_up();
- X indent_puts( "yy_c = yy_meta[yy_c];" );
- X indent_down();
- X }
- X
- X indent_puts( "}" );
- X indent_down();
- X
- X indent_puts(
- X "yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];" );
- X }
- X
- X
- X/* generate the code to find the next match */
- X
- Xvoid gen_next_match()
- X
- X {
- X /* NOTE - changes in here should be reflected in gen_next_state() and
- X * gen_NUL_trans()
- X */
- X char *char_map = useecs ? "yy_ec[*yy_cp]" : "*yy_cp";
- X char *char_map_2 = useecs ? "yy_ec[*++yy_cp]" : "*++yy_cp";
- X
- X if ( fulltbl )
- X {
- X indent_put2s(
- X "while ( (yy_current_state = yy_nxt[yy_current_state][%s]) > 0 )",
- X char_map );
- X
- X indent_up();
- X
- X if ( num_backtracking > 0 )
- X {
- X indent_puts( "{" );
- X gen_backtracking();
- X putchar( '\n' );
- X }
- X
- X indent_puts( "++yy_cp;" );
- X
- X if ( num_backtracking > 0 )
- X indent_puts( "}" );
- X
- X indent_down();
- X
- X putchar( '\n' );
- X indent_puts( "yy_current_state = -yy_current_state;" );
- X }
- X
- X else if ( fullspd )
- X {
- X indent_puts( "{" );
- X indent_puts( "register const struct yy_trans_info *yy_trans_info;\n" );
- X indent_puts( "register YY_CHAR yy_c;\n" );
- X indent_put2s( "for ( yy_c = %s;", char_map );
- X indent_puts(
- X " (yy_trans_info = &yy_current_state[yy_c])->yy_verify == yy_c;" );
- X indent_put2s( " yy_c = %s )", char_map_2 );
- X
- X indent_up();
- X
- X if ( num_backtracking > 0 )
- X indent_puts( "{" );
- X
- X indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
- X
- X if ( num_backtracking > 0 )
- X {
- X putchar( '\n' );
- X gen_backtracking();
- X indent_puts( "}" );
- X }
- X
- X indent_down();
- X indent_puts( "}" );
- X }
- X
- X else
- X { /* compressed */
- X indent_puts( "do" );
- X
- X indent_up();
- X indent_puts( "{" );
- X
- X gen_next_state( false );
- X
- X indent_puts( "++yy_cp;" );
- X
- X indent_puts( "}" );
- X indent_down();
- X
- X do_indent();
- X
- X if ( interactive )
- X printf( "while ( yy_base[yy_current_state] != %d );\n", jambase );
- X else
- X printf( "while ( yy_current_state != %d );\n", jamstate );
- X
- X if ( ! reject && ! interactive )
- X {
- X /* do the guaranteed-needed backtrack to figure out the match */
- X indent_puts( "yy_cp = yy_last_accepting_cpos;" );
- X indent_puts( "yy_current_state = yy_last_accepting_state;" );
- X }
- X }
- X }
- X
- X
- X/* generate the code to find the next state */
- X
- Xvoid gen_next_state( worry_about_NULs )
- Xint worry_about_NULs;
- X
- X { /* NOTE - changes in here should be reflected in get_next_match() */
- X char char_map[256];
- X
- X if ( worry_about_NULs && ! nultrans )
- X {
- X if ( useecs )
- X (void) sprintf( char_map, "(*yy_cp ? yy_ec[*yy_cp] : %d)", NUL_ec );
- X else
- X (void) sprintf( char_map, "(*yy_cp ? *yy_cp : %d)", NUL_ec );
- X }
- X
- X else
- X (void) strcpy( char_map, useecs ? "yy_ec[*yy_cp]" : "*yy_cp" );
- X
- X if ( worry_about_NULs && nultrans )
- X {
- X if ( ! fulltbl && ! fullspd )
- X /* compressed tables backtrack *before* they match */
- X gen_backtracking();
- X
- X indent_puts( "if ( *yy_cp )" );
- X indent_up();
- X indent_puts( "{" );
- X }
- X
- X if ( fulltbl )
- X indent_put2s( "yy_current_state = yy_nxt[yy_current_state][%s];",
- X char_map );
- X
- X else if ( fullspd )
- X indent_put2s( "yy_current_state += yy_current_state[%s].yy_nxt;",
- X char_map );
- X
- X else
- X gen_next_compressed_state( char_map );
- X
- X if ( worry_about_NULs && nultrans )
- X {
- X indent_puts( "}" );
- X indent_down();
- X indent_puts( "else" );
- X indent_up();
- X indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
- X indent_down();
- X }
- X
- X if ( fullspd || fulltbl )
- X gen_backtracking();
- X
- X if ( reject )
- X indent_puts( "*yy_state_ptr++ = yy_current_state;" );
- X }
- X
- X
- X/* generate the code to make a NUL transition */
- X
- Xvoid gen_NUL_trans()
- X
- X { /* NOTE - changes in here should be reflected in get_next_match() */
- X int need_backtracking = (num_backtracking > 0 && ! reject);
- X
- X if ( need_backtracking )
- X /* we'll need yy_cp lying around for the gen_backtracking() */
- X indent_puts( "register YY_CHAR *yy_cp = yy_c_buf_p;" );
- X
- X putchar( '\n' );
- X
- X if ( nultrans )
- X {
- X indent_puts( "yy_current_state = yy_NUL_trans[yy_current_state];" );
- X indent_puts( "yy_is_jam = (yy_current_state == 0);" );
- X }
- X
- X else if ( fulltbl )
- X {
- X do_indent();
- X printf( "yy_current_state = yy_nxt[yy_current_state][%d];\n",
- X NUL_ec );
- X indent_puts( "yy_is_jam = (yy_current_state <= 0);" );
- X }
- X
- X else if ( fullspd )
- X {
- X do_indent();
- X printf( "register int yy_c = %d;\n", NUL_ec );
- X
- X indent_puts(
- X "register const struct yy_trans_info *yy_trans_info;\n" );
- X indent_puts( "yy_trans_info = &yy_current_state[yy_c];" );
- X indent_puts( "yy_current_state += yy_trans_info->yy_nxt;" );
- X
- X indent_puts( "yy_is_jam = (yy_trans_info->yy_verify != yy_c);" );
- X }
- X
- X else
- X {
- X char NUL_ec_str[20];
- X
- X (void) sprintf( NUL_ec_str, "%d", NUL_ec );
- X gen_next_compressed_state( NUL_ec_str );
- X
- X if ( reject )
- X indent_puts( "*yy_state_ptr++ = yy_current_state;" );
- X
- X do_indent();
- X
- X if ( interactive )
- X printf( "yy_is_jam = (yy_base[yy_current_state] == %d);\n",
- X jambase );
- X else
- X printf( "yy_is_jam = (yy_current_state == %d);\n", jamstate );
- X }
- X
- X /* if we've entered an accepting state, backtrack; note that
- X * compressed tables have *already* done such backtracking, so
- X * we needn't bother with it again
- X */
- X if ( need_backtracking && (fullspd || fulltbl) )
- X {
- X putchar( '\n' );
- X indent_puts( "if ( ! yy_is_jam )" );
- X indent_up();
- X indent_puts( "{" );
- X gen_backtracking();
- X indent_puts( "}" );
- X indent_down();
- X }
- X }
- X
- X
- X/* generate the code to find the start state */
- X
- Xvoid gen_start_state()
- X
- X {
- X if ( fullspd )
- X indent_put2s( "yy_current_state = yy_start_state_list[yy_start%s];",
- X bol_needed ? " + (yy_bp[-1] == '\\n' ? 1 : 0)" : "" );
- X
- X else
- X {
- X indent_puts( "yy_current_state = yy_start;" );
- X
- X if ( bol_needed )
- X {
- X indent_puts( "if ( yy_bp[-1] == '\\n' )" );
- X indent_up();
- X indent_puts( "++yy_current_state;" );
- X indent_down();
- X }
- X
- X if ( reject )
- X {
- X /* set up for storing up states */
- X indent_puts( "yy_state_ptr = yy_state_buf;" );
- X indent_puts( "*yy_state_ptr++ = yy_current_state;" );
- X }
- X }
- X }
- X
- X
- X/* gentabs - generate data statements for the transition tables
- X *
- X * synopsis
- X * gentabs();
- X */
- X
- Xvoid gentabs()
- X
- X {
- X int i, j, k, *accset, nacc, *acc_array, total_states;
- X int end_of_buffer_action = num_rules + 1;
- X
- X /* *everything* is done in terms of arrays starting at 1, so provide
- X * a null entry for the zero element of all C arrays
- X */
- X static char C_char_decl[] =
- X "static const YY_CHAR %s[%d] =\n { 0,\n";
- X
- X acc_array = allocate_integer_array( current_max_dfas );
- X nummt = 0;
- X
- X /* the compressed table format jams by entering the "jam state",
- X * losing information about the previous state in the process.
- X * In order to recover the previous state, we effectively need
- X * to keep backtracking information.
- X */
- X ++num_backtracking;
- X
- X if ( reject )
- X {
- X /* write out accepting list and pointer list
- X *
- X * first we generate the "yy_acclist" array. In the process, we compute
- X * the indices that will go into the "yy_accept" array, and save the
- X * indices in the dfaacc array
- X */
- X int EOB_accepting_list[2];
- X
- X /* set up accepting structures for the End Of Buffer state */
- X EOB_accepting_list[0] = 0;
- X EOB_accepting_list[1] = end_of_buffer_action;
- X accsiz[end_of_buffer_state] = 1;
- X dfaacc[end_of_buffer_state].dfaacc_set = EOB_accepting_list;
- X
- X printf( C_short_decl, "yy_acclist", max( numas, 1 ) + 1 );
- X
- X j = 1; /* index into "yy_acclist" array */
- X
- X for ( i = 1; i <= lastdfa; ++i )
- X {
- X acc_array[i] = j;
- X
- X if ( accsiz[i] != 0 )
- X {
- X accset = dfaacc[i].dfaacc_set;
- X nacc = accsiz[i];
- X
- X if ( trace )
- X fprintf( stderr, "state # %d accepts: ", i );
- X
- X for ( k = 1; k <= nacc; ++k )
- X {
- X int accnum = accset[k];
- X
- X ++j;
- X
- X if ( variable_trailing_context_rules &&
- X ! (accnum & YY_TRAILING_HEAD_MASK) &&
- X accnum > 0 &&
- X rule_type[accnum] == RULE_VARIABLE )
- X {
- X /* special hack to flag accepting number as part
- X * of trailing context rule
- X */
- X accnum |= YY_TRAILING_MASK;
- X }
- X
- X mkdata( accnum );
- X
- X if ( trace )
- X {
- X fprintf( stderr, "[%d]", accset[k] );
- X
- X if ( k < nacc )
- X fputs( ", ", stderr );
- X else
- X putc( '\n', stderr );
- X }
- X }
- X }
- X }
- X
- X /* add accepting number for the "jam" state */
- X acc_array[i] = j;
- X
- X dataend();
- X }
- X
- X else
- X {
- X dfaacc[end_of_buffer_state].dfaacc_state = end_of_buffer_action;
- X
- X for ( i = 1; i <= lastdfa; ++i )
- X acc_array[i] = dfaacc[i].dfaacc_state;
- X
- X /* add accepting number for jam state */
- X acc_array[i] = 0;
- X }
- X
- X /* spit out "yy_accept" array. If we're doing "reject", it'll be pointers
- X * into the "yy_acclist" array. Otherwise it's actual accepting numbers.
- X * In either case, we just dump the numbers.
- X */
- X
- X /* "lastdfa + 2" is the size of "yy_accept"; includes room for C arrays
- X * beginning at 0 and for "jam" state
- X */
- X k = lastdfa + 2;
- X
- X if ( reject )
- X /* we put a "cap" on the table associating lists of accepting
- X * numbers with state numbers. This is needed because we tell
- X * where the end of an accepting list is by looking at where
- X * the list for the next state starts.
- X */
- X ++k;
- X
- X printf( C_short_decl, "yy_accept", k );
- X
- X for ( i = 1; i <= lastdfa; ++i )
- X {
- X mkdata( acc_array[i] );
- X
- X if ( ! reject && trace && acc_array[i] )
- X fprintf( stderr, "state # %d accepts: [%d]\n", i, acc_array[i] );
- X }
- X
- X /* add entry for "jam" state */
- X mkdata( acc_array[i] );
- X
- X if ( reject )
- X /* add "cap" for the list */
- X mkdata( acc_array[i] );
- X
- X dataend();
- X
- X if ( useecs )
- X genecs();
- X
- X if ( usemecs )
- X {
- X /* write out meta-equivalence classes (used to index templates with) */
- X
- X if ( trace )
- X fputs( "\n\nMeta-Equivalence Classes:\n", stderr );
- X
- X printf( C_char_decl, "yy_meta", numecs + 1 );
- X
- X for ( i = 1; i <= numecs; ++i )
- X {
- X if ( trace )
- X fprintf( stderr, "%d = %d\n", i, abs( tecbck[i] ) );
- X
- X mkdata( abs( tecbck[i] ) );
- X }
- X
- X dataend();
- X }
- X
- X total_states = lastdfa + numtemps;
- X
- X printf( tblend > MAX_SHORT ? C_long_decl : C_short_decl,
- X "yy_base", total_states + 1 );
- X
- X for ( i = 1; i <= lastdfa; ++i )
- X {
- X register int d = def[i];
- X
- X if ( base[i] == JAMSTATE )
- X base[i] = jambase;
- X
- X if ( d == JAMSTATE )
- X def[i] = jamstate;
- X
- X else if ( d < 0 )
- X {
- X /* template reference */
- X ++tmpuses;
- X def[i] = lastdfa - d + 1;
- X }
- X
- X mkdata( base[i] );
- X }
- X
- X /* generate jam state's base index */
- X mkdata( base[i] );
- X
- X for ( ++i /* skip jam state */; i <= total_states; ++i )
- X {
- X mkdata( base[i] );
- X def[i] = jamstate;
- X }
- X
- X dataend();
- X
- X printf( tblend > MAX_SHORT ? C_long_decl : C_short_decl,
- X "yy_def", total_states + 1 );
- X
- X for ( i = 1; i <= total_states; ++i )
- X mkdata( def[i] );
- X
- X dataend();
- X
- X printf( lastdfa > MAX_SHORT ? C_long_decl : C_short_decl,
- X "yy_nxt", tblend + 1 );
- X
- X for ( i = 1; i <= tblend; ++i )
- X {
- X if ( nxt[i] == 0 || chk[i] == 0 )
- X nxt[i] = jamstate; /* new state is the JAM state */
- X
- X mkdata( nxt[i] );
- X }
- X
- X dataend();
- X
- X printf( lastdfa > MAX_SHORT ? C_long_decl : C_short_decl,
- X "yy_chk", tblend + 1 );
- X
- X for ( i = 1; i <= tblend; ++i )
- X {
- X if ( chk[i] == 0 )
- X ++nummt;
- X
- X mkdata( chk[i] );
- X }
- X
- X dataend();
- X }
- X
- X
- X/* write out a formatted string (with a secondary string argument) at the
- X * current indentation level, adding a final newline
- X */
- X
- Xvoid indent_put2s( fmt, arg )
- Xchar fmt[], arg[];
- X
- X {
- X do_indent();
- X printf( fmt, arg );
- X putchar( '\n' );
- X }
- X
- X
- X/* write out a string at the current indentation level, adding a final
- X * newline
- X */
- X
- Xvoid indent_puts( str )
- Xchar str[];
- X
- X {
- X do_indent();
- X puts( str );
- X }
- X
- X
- X/* make_tables - generate transition tables
- X *
- X * synopsis
- X * make_tables();
- X *
- X * Generates transition tables and finishes generating output file
- X */
- X
- Xvoid make_tables()
- X
- X {
- X register int i;
- X int did_eof_rule = false;
- X
- X skelout();
- X
- X /* first, take care of YY_DO_BEFORE_ACTION depending on yymore being used */
- X set_indent( 2 );
- X
- X if ( yymore_used )
- X {
- X indent_puts( "yytext -= yy_more_len; \\" );
- X indent_puts( "yyleng = yy_cp - yytext; \\" );
- X }
- X
- X else
- X indent_puts( "yyleng = yy_cp - yy_bp; \\" );
- X
- X set_indent( 0 );
- X
- X skelout();
- X
- X
- X printf( "#define YY_END_OF_BUFFER %d\n", num_rules + 1 );
- X
- X if ( fullspd )
- X { /* need to define the transet type as a size large
- X * enough to hold the biggest offset
- X */
- X int total_table_size = tblend + numecs + 1;
- X char *trans_offset_type =
- X total_table_size > MAX_SHORT ? "long" : "short";
- X
- X set_indent( 0 );
- X indent_puts( "struct yy_trans_info" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "short yy_verify;" );
- X
- X /* in cases where its sister yy_verify *is* a "yes, there is a
- X * transition", yy_nxt is the offset (in records) to the next state.
- X * In most cases where there is no transition, the value of yy_nxt
- X * is irrelevant. If yy_nxt is the -1th record of a state, though,
- X * then yy_nxt is the action number for that state
- X */
- X
- X indent_put2s( "%s yy_nxt;", trans_offset_type );
- X indent_puts( "};" );
- X indent_down();
- X
- X indent_puts( "typedef const struct yy_trans_info *yy_state_type;" );
- X }
- X
- X else
- X indent_puts( "typedef int yy_state_type;" );
- X
- X if ( fullspd )
- X genctbl();
- X
- X else if ( fulltbl )
- X genftbl();
- X
- X else
- X gentabs();
- X
- X if ( num_backtracking > 0 )
- X {
- X indent_puts( "static yy_state_type yy_last_accepting_state;" );
- X indent_puts( "static YY_CHAR *yy_last_accepting_cpos;\n" );
- X }
- X
- X if ( nultrans )
- X {
- X printf( C_state_decl, "yy_NUL_trans", lastdfa + 1 );
- X
- X for ( i = 1; i <= lastdfa; ++i )
- X {
- X if ( fullspd )
- X {
- X if ( nultrans )
- X printf( " &yy_transition[%d],\n", base[i] );
- X else
- X printf( " 0,\n" );
- X }
- X
- X else
- X mkdata( nultrans[i] );
- X }
- X
- X dataend();
- X }
- X
- X if ( ddebug )
- X { /* spit out table mapping rules to line numbers */
- X indent_puts( "extern int yy_flex_debug;" );
- X indent_puts( "int yy_flex_debug = 1;\n" );
- X
- X printf( C_short_decl, "yy_rule_linenum", num_rules );
- X for ( i = 1; i < num_rules; ++i )
- X mkdata( rule_linenum[i] );
- X dataend();
- X }
- X
- X if ( reject )
- X {
- X /* declare state buffer variables */
- X puts(
- X "static yy_state_type yy_state_buf[YY_BUF_SIZE + 2], *yy_state_ptr;" );
- X puts( "static YY_CHAR *yy_full_match;" );
- X puts( "static int yy_lp;" );
- X
- X if ( variable_trailing_context_rules )
- X {
- X puts( "static int yy_looking_for_trail_begin = 0;" );
- X puts( "static int yy_full_lp;" );
- X puts( "static int *yy_full_state;" );
- X printf( "#define YY_TRAILING_MASK 0x%x\n", YY_TRAILING_MASK );
- X printf( "#define YY_TRAILING_HEAD_MASK 0x%x\n",
- X YY_TRAILING_HEAD_MASK );
- X }
- X
- X puts( "#define REJECT \\" );
- X puts( "{ \\" );
- X puts(
- X "*yy_cp = yy_hold_char; /* undo effects of setting up yytext */ \\" );
- X puts(
- X "yy_cp = yy_full_match; /* restore poss. backed-over text */ \\" );
- X
- X if ( variable_trailing_context_rules )
- X {
- X puts( "yy_lp = yy_full_lp; /* restore orig. accepting pos. */ \\" );
- X puts(
- X "yy_state_ptr = yy_full_state; /* restore orig. state */ \\" );
- X puts(
- X "yy_current_state = *yy_state_ptr; /* restore curr. state */ \\" );
- X }
- X
- X puts( "++yy_lp; \\" );
- X puts( "goto find_rule; \\" );
- X puts( "}" );
- X }
- X
- X else
- X {
- X puts( "/* the intent behind this definition is that it'll catch" );
- X puts( " * any uses of REJECT which flex missed" );
- X puts( " */" );
- X puts( "#define REJECT reject_used_but_not_detected" );
- X }
- X
- X if ( yymore_used )
- X {
- X indent_puts( "static int yy_more_flag = 0;" );
- X indent_puts( "static int yy_doing_yy_more = 0;" );
- X indent_puts( "static int yy_more_len = 0;" );
- X indent_puts(
- X "#define yymore() { yy_more_flag = 1; }" );
- X indent_puts(
- X "#define YY_MORE_ADJ (yy_doing_yy_more ? yy_more_len : 0)" );
- X }
- X
- X else
- X {
- X indent_puts( "#define yymore() yymore_used_but_not_detected" );
- X indent_puts( "#define YY_MORE_ADJ 0" );
- X }
- X
- X skelout();
- X
- X if ( ferror( temp_action_file ) )
- X flexfatal( "error occurred when writing temporary action file" );
- X
- X else if ( fclose( temp_action_file ) )
- X flexfatal( "error occurred when closing temporary action file" );
- X
- X temp_action_file = fopen( action_file_name, "r" );
- X
- X if ( temp_action_file == NULL )
- X flexfatal( "could not re-open temporary action file" );
- X
- X /* copy prolog from action_file to output file */
- X action_out();
- X
- X skelout();
- X
- X set_indent( 2 );
- X
- X if ( yymore_used )
- X {
- X indent_puts( "yy_doing_yy_more = yy_more_flag;" );
- X indent_puts( "if ( yy_doing_yy_more )" );
- X indent_up();
- X indent_puts( "{" );
- X indent_puts( "yy_more_len = yyleng;" );
- X indent_puts( "yy_more_flag = 0;" );
- X indent_puts( "}" );
- X indent_down();
- X }
- X
- X skelout();
- X
- X gen_start_state();
- X
- X /* note, don't use any indentation */
- X puts( "yy_match:" );
- X gen_next_match();
- X
- X skelout();
- X set_indent( 2 );
- X gen_find_action();
- X
- X skelout();
- X if ( ddebug )
- X {
- X indent_puts( "if ( yy_flex_debug )" );
- X indent_up();
- X
- X indent_puts( "{" );
- X indent_puts( "if ( yy_act == 0 )" );
- X indent_up();
- X indent_puts( "fprintf( stderr, \"--scanner backtracking\\n\" );" );
- X indent_down();
- X
- X do_indent();
- X printf( "else if ( yy_act < %d )\n", num_rules );
- X indent_up();
- X indent_puts(
- X "fprintf( stderr, \"--accepting rule at line %d (\\\"%s\\\")\\n\"," );
- X indent_puts( " yy_rule_linenum[yy_act], yytext );" );
- X indent_down();
- X
- X do_indent();
- X printf( "else if ( yy_act == %d )\n", num_rules );
- X indent_up();
- X indent_puts(
- X "fprintf( stderr, \"--accepting default rule (\\\"%s\\\")\\n\"," );
- X indent_puts( " yytext );" );
- X indent_down();
- X
- X do_indent();
- X printf( "else if ( yy_act == %d )\n", num_rules + 1 );
- X indent_up();
- X indent_puts( "fprintf( stderr, \"--(end of buffer or a NUL)\\n\" );" );
- X indent_down();
- X
- X do_indent();
- X printf( "else\n" );
- X indent_up();
- X indent_puts( "fprintf( stderr, \"--EOF\\n\" );" );
- X indent_down();
- X
- X indent_puts( "}" );
- X indent_down();
- X }
- X
- X /* copy actions from action_file to output file */
- X skelout();
- X indent_up();
- X gen_bt_action();
- X action_out();
- X
- X /* generate cases for any missing EOF rules */
- X for ( i = 1; i <= lastsc; ++i )
- X if ( ! sceof[i] )
- X {
- X do_indent();
- X printf( "case YY_STATE_EOF(%s):\n", scname[i] );
- X did_eof_rule = true;
- X }
- X
- X if ( did_eof_rule )
- X {
- X indent_up();
- X indent_puts( "yyterminate();" );
- X indent_down();
- X }
- X
- X
- X /* generate code for handling NUL's, if needed */
- X
- X /* first, deal with backtracking and setting up yy_cp if the scanner
- X * finds that it should JAM on the NUL
- X */
- X skelout();
- X set_indent( 7 );
- X
- X if ( fullspd || fulltbl )
- X indent_puts( "yy_cp = yy_c_buf_p;" );
- X
- X else
- X { /* compressed table */
- X if ( ! reject && ! interactive )
- X {
- X /* do the guaranteed-needed backtrack to figure out the match */
- X indent_puts( "yy_cp = yy_last_accepting_cpos;" );
- X indent_puts( "yy_current_state = yy_last_accepting_state;" );
- X }
- X }
- X
- X
- X /* generate code for yy_get_previous_state() */
- X set_indent( 1 );
- X skelout();
- X
- X if ( bol_needed )
- X indent_puts( "register YY_CHAR *yy_bp = yytext;\n" );
- X
- X gen_start_state();
- X
- X set_indent( 2 );
- X skelout();
- X gen_next_state( true );
- X
- X set_indent( 1 );
- X skelout();
- X gen_NUL_trans();
- X
- X skelout();
- X
- X /* copy remainder of input to output */
- X
- X line_directive_out( stdout );
- X (void) flexscan(); /* copy remainder of input to output */
- X }
- END_OF_FILE
- if test 31559 -ne `wc -c <'gen.c'`; then
- echo shar: \"'gen.c'\" unpacked with wrong size!
- fi
- # end of 'gen.c'
- fi
- echo shar: End of archive 7 \(of 13\).
- cp /dev/null ark7isdone
- MISSING=""
- for I in 1 2 3 4 5 6 7 8 9 10 11 12 13 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked all 13 archives.
- rm -f ark[1-9]isdone ark[1-9][0-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
- --
- Mail submissions (sources or binaries) to <amiga@uunet.uu.net>.
- Mail comments to the moderator at <amiga-request@uunet.uu.net>.
- Post requests for sources, and general discussion to comp.sys.amiga.
-